﻿using Newtonsoft.Json;
using System;

namespace BLL
{
    /// <summary>
    /// Json转换类
    /// </summary>
    class JsonHelper
    {
        #region 对象-->字符串
        public static string EntityToStr<T>(T entity)
        {
            string res = string.Empty;
            //entity = default(T);        
            try
            {
                res = JsonConvert.SerializeObject(entity);
            }
            catch (Exception)
            {
                entity = default(T);
            }
            return res;
        }
        #endregion

        #region 字符串-->对象
        public static T StrToEntity<T>(string res)
        {
            T eatity = default(T);//初始化定义类型
            try
            {
                eatity = (T)JsonConvert.DeserializeObject(res, typeof(T));
            }
            catch (Exception)
            {
                eatity = default(T);
            }
            return eatity;
        }
        #endregion
    }
}
